package com.qidian.mespro.service.impl;

import com.qidian.mespro.mapper.BomMapper;
import com.qidian.mespro.mapper.ProductOrderMapper;
import com.qidian.mespro.service.BomService;
import com.qidianit.mes.pojo.Bom;
import com.qidianit.mes.pojo.ProductOrder;
import com.qidianit.mes.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ author: 周晓轩
 * @ company 起点编程
 * @ time: 2025/9/23 - 20:00
 * @ Description:
 */
@Service
public class BomServiceImpl implements BomService {

     @Autowired
     private BomMapper bomMapper;

     @Autowired
     private ProductOrderMapper productOrderMapper;

     @Override
     public Result<List<Bom>> getBomListByProductionOrderCode(String productionOrderCode) {

      int count = productOrderMapper.countByCode(productionOrderCode);
      if (count == 0) {
       return Result.error("工单不存在");
      }

      ProductOrder productOrder = productOrderMapper.selectByCode(productionOrderCode);
      if (productOrder == null) {
       return Result.error("工单信息获取失败");
      }

      // 根据主物料编码查询BOM组成
      List<Bom> bomList = bomMapper.selectByMainMaterialCode(productOrder.getMaterialCode());
      return Result.success(bomList);
     }
}
